"""
Uniform command form interface.
"""
import argparse
import os
import sys
from mispconn.tm import SampleUploader
from mispconn.pf import AttachmentDownloader
from androidsandbox.manager import AndroidManager

TM_DATA_TYPES = ['malware', 'traffic']


def tm(args):
    """For tm(to misp) command. """
    if not(os.path.isdir(args.path) or os.path.isfile(args.path)):
        sys.exit(f'{args.path} is not a valid path.')
    if args.data_type == 'malware':
        SampleUploader(args.path, args.tags, args.delete, args.file_type,
                       args.file_paths_for_excluding)

def asb(args):
    """For Android Sandbox command."""
    AndroidManager(args.tags, args.num).execute()


def pf(args):
    """Pull files form misp"""
    AttachmentDownloader(args.attribute_type, args.path, args.tags, args.num,
                         args.file_paths_for_excluding).execute()


parser = argparse.ArgumentParser(prog='AICTI')
subparsers = parser.add_subparsers(help='sub-command help')

# tm(to misp) command
parser_tm = subparsers.add_parser('tm', help='upload data to misp')
parser_tm.add_argument('--data-type', metavar='<type>', dest='data_type',
                       choices=TM_DATA_TYPES, default='malware',
                       help='data type: {}'.format('|'.join(TM_DATA_TYPES)))
parser_tm.add_argument('--file-type', metavar='<type>', dest='file_type',
                       nargs='+', default='all',
                       help='only upload this type of files')
parser_tm.add_argument('--path', required=True, metavar='<path>',
                       help='data path')
parser_tm.add_argument('--tags',  metavar='<tag>', nargs='+', help='misp tags')
parser_tm.add_argument('--delete', action='store_true',
                       help='delete origin samples after upload')
parser_tm.add_argument('--exclude-from-files', metavar='<file>', nargs='+',
                       dest='file_paths_for_excluding',
                       help='exclude paths in the file')
parser_tm.set_defaults(func=tm)

# Android Sandbox command
parser_asb = subparsers.add_parser('asb', help='android sandbox with misp')
parser_asb.add_argument('--tags',  metavar='<tag>', nargs='+', help='misp tags, format: and/or/not:<tag>')
parser_asb.add_argument('--num',  metavar='<num>',  type=int, default=100,
                       help='number of samples')
parser_asb.set_defaults(func=asb)


# Pull File command (from MISP)
parser_pf = subparsers.add_parser('pf', help='pull files from misp')
parser_pf.add_argument('--attribute-type',  metavar='<type>', required=True,
                       dest='attribute_type', help='misp attribute type')
parser_pf.add_argument('--path', required=True, metavar='<path>',
                       help='path for saving downloads')
parser_pf.add_argument('--tags',  metavar='<tag>', nargs='+',
                       help='misp tags on both events and attributes, format: and/or/not:<tag>')
parser_pf.add_argument('--num',  metavar='<num>',  type=int, default=100,
                       help='number of files')
parser_pf.add_argument('--exclude-from-files', metavar='<file>', nargs='+',
                       dest='file_paths_for_excluding',
                       help='exclude paths in the files')
parser_pf.set_defaults(func=pf)

all_args = parser.parse_args()
all_args.func(all_args)
